package com.c2.web.backend;

import com.c2.entity.EbProductCategory;
import com.c2.service.ProductCategoryService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author Stars Lee
 * @date 2020/9/16 - 15:17
 */

@Controller
@RequestMapping("/productClass")//控制类
public class ProductCategoryController {
    @Autowired
    private ProductCategoryService productCategoryService;

//    @RequestMapping("/Categorylist")//分页查询操作
//    public String list(Model model){
//        List<EbProductCategory> productCategoryList=productCategoryService.listByPage(page,6);
//        model.addAttribute("productCategoryList",productCategoryList);
//        Integer maxPage=productCategoryService.findMaxPage(pageSize);
//        model.addAttribute("maxPage",maxPage);
//        Integer currentPage=page;
//        model.addAttribute("currentPage",currentPage);
//        return "manage/productClass";
//    }

    @RequestMapping("/bigCategoryList")//查询大类
    public String bigCategoryList(Model model){
        List<EbProductCategory> bigClassList=productCategoryService.bigClassList();
        model.addAttribute("bigClassList",bigClassList);
        return "manage/productClass";
    }
    @RequestMapping("/smallCategoryList")//查询小类
    public String smallCategoryList(Model model,
                                    @RequestParam(value = "parentId") Integer parentId){
        List<EbProductCategory> smallClassList=productCategoryService.smallClassList(parentId);
        model.addAttribute("smallClassList",smallClassList);
        return "manage/smallProductClass";
    }
    @RequestMapping("/bigCategoryListByPage")//分页查询大类
    public String bigCategoryListByPage(Model model,
                                        @RequestParam(value = "page",defaultValue="1")Integer page,
                                        @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        List<EbProductCategory> bigClassList=productCategoryService.bigClassListByPage(page,pageSize);
        model.addAttribute("bigClassList",bigClassList);
        Integer bigMaxPage=productCategoryService.findBigAllPage(pageSize);
        model.addAttribute("bigMaxPage",bigMaxPage);
        Integer bigCurrent=page;
        model.addAttribute("bigCurrent",bigCurrent);
        return "manage/productClass";
    }
    @RequestMapping("/smallCategoryListByPage")//分页查询小类
    public String smallCategoryListByPage(Model model,
                                          @RequestParam(value = "parentId") Integer parentId,
                                          @RequestParam(value = "page",defaultValue="1")Integer page,
                                          @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        List<EbProductCategory> smallClassList=productCategoryService.smallClassListByPage(parentId,page,pageSize);
        model.addAttribute("smallClassList",smallClassList);
        Integer smallMaxPage=productCategoryService.findSmallAllPage(parentId,pageSize);
        model.addAttribute("smallMaxPage",smallMaxPage);
        Integer smallCurrent=page;
        model.addAttribute("smallCurrent",smallCurrent);
        model.addAttribute("parentId",parentId);
        return "manage/smallProductClass";
    }


    @RequestMapping("/to_save")//添加页面
    public String toSave(){

        return "manage/productClass-add";
    }

    @RequestMapping("save_commit")//添加提交跳转判断
    public String toCommit(EbProductCategory ebProductCategory){
        if ((ebProductCategory.getEpcId()==0) || (ebProductCategory.getEpcName().trim().length()==0) ||
                (ebProductCategory.getEpcParentId()==0)){
            return "manage/manage-productClassAddError";
        }else {
            Integer result=productCategoryService.toSaveProductCategory(ebProductCategory);
            if (result<=0){
                return "manage/manage-productClassAddError";
            }else {
                return "manage/manage-productClassAddSuccessfully";
            }
        }
    }

    @RequestMapping("/to_smallSave")//添加页面
    public String toSmallSave(){

        return "manage/SmallProductClass-add";
    }

    @RequestMapping("save_smallCommit")//添加提交跳转判断
    public String toSmallCommit(Model model,EbProductCategory ebProductCategory){
        if ((ebProductCategory.getEpcId()==0) || (ebProductCategory.getEpcName().trim().length()==0) ||
                (ebProductCategory.getEpcParentId()==0)){
            return "manage/manage-smallProductClassAddError";
        }else {
            Integer result=productCategoryService.toSaveProductCategory(ebProductCategory);
            if (result<=0){
                return "manage/manage-smallProductClassAddError";
            }else {
                model.addAttribute("paarentId",ebProductCategory.getEpcParentId());
                return "manage/manage-smallProductClassAddSuccessfully";
            }
        }
    }

    @RequestMapping("/to_delete")
    public String toDelete(@RequestParam(value = "epcId")Integer epcId){
        Integer res=productCategoryService.toDeleteProductCategory(epcId);

        if (res<0){
            return " ";
        }else {
            return "manage/manage-productClassDeleteSuccessfully";
        }

    }
    @RequestMapping("/to_smallDelete")
    public String toSmallDelete(Model model,
                                @RequestParam(value = "epcId")Integer epcId,
                                @RequestParam(value = "parentId")Integer parentId){
        Integer res=productCategoryService.toDeleteProductCategory(epcId);

        if (res<0){
            return " ";
        }else {
            model.addAttribute("parentId",parentId);
            return "manage/manage-smallProductClassDeleteSuccessfully";
        }

    }

    @RequestMapping("/to_update")//修改页面
    public String toUpdate(Model model,@RequestParam(value = "epcId") Integer epcId){
        List<EbProductCategory> productCategoryUpdated=productCategoryService.selectByCondition(epcId);

        model.addAttribute("productCategoryUpdated",productCategoryUpdated);
        return "manage/productClass-modify";
    }
    @RequestMapping("/update_submit")//修改提交跳转判断
    public String updateSubmit(Model model,EbProductCategory ebProductCategory){
        if ((ebProductCategory.getEpcId()==0) || (ebProductCategory.getEpcName().trim().length()==0) ||
                (ebProductCategory.getEpcParentId()==0)){
            model.addAttribute("epcId",ebProductCategory.getEpcId());
            return "manage/manage-productClassUpdateError";
        }else {
            Integer result=productCategoryService.toUpdateProductCategory(ebProductCategory);
            if (result<=0){
                model.addAttribute("epcId",ebProductCategory.getEpcId());
                return "manage/manage-productClassUpdateError";
            }else {
                return "manage/manage-productClassUpdateSuccessfully";
            }
        }
    }

    @RequestMapping("/to_smallUpdate")//修改页面
    public String toSmallUpdate(Model model,@RequestParam(value = "epcId") Integer epcId){
        List<EbProductCategory> productCategoryUpdated=productCategoryService.selectByCondition(epcId);

        model.addAttribute("productCategoryUpdated",productCategoryUpdated);
        return "manage/smallProductClass-modify";
    }
    @RequestMapping("/update_smallSubmit")//修改提交跳转判断
    public String updateSmallSubmit(Model model,EbProductCategory ebProductCategory){
        if ((ebProductCategory.getEpcId()==0) || (ebProductCategory.getEpcName().trim().length()==0) ||
                (ebProductCategory.getEpcParentId()==0)){
            model.addAttribute("epcId",ebProductCategory.getEpcId());
            return "manage/manage-smallProductClassUpdateError";
        }else {
            Integer result=productCategoryService.toUpdateProductCategory(ebProductCategory);
            if (result<=0){
                model.addAttribute("epcId",ebProductCategory.getEpcId());
                return "manage/manage-smallProductClassUpdateError";
            }else {
                model.addAttribute("parentId",ebProductCategory.getEpcParentId());
                return "manage/manage-smallProductClassUpdateSuccessfully";
            }
        }
    }
}
